import request from '@/request';
import store from '../store/index';

export function _post(apiRoute, data) {
  console.log('s-s', store.state);
  if (store.state.session) {
    data.session = store.state.session;
  } else if (getCookie('session')) {
    data.session = getCookie('session');
  }
  if (store.state.openid) {
    data.openid = store.state.openid;
  } else if (getCookie('openid')) {
    data.openid = getCookie('openid');
  }

  if (store.state.applet_id) {
    data.applet_id = store.state.applet_id;
  }
  if (store.state.shop) {
    data.shop_id = store.state.shop.id;
  }

  // data.applet_id = 1;
  // data.openid = 'ozkF0wNDtLI2s4tf_dLO8aVEnOdM';
  // data.session = '16_ErUEM-Ake7xSvQGAEqR9r8mBUChfcoWneIAjOSQ42FUvLNx47jazfsm99NojEmwoL6xioHGR9zZRkYZhLAq-VQ';
  // data.shop_id = 1;

  return request({
    url: apiRoute,
    method: 'post',
    data: data
  });
}

function getCookie(name) {
  let arr = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  if (arr === document.cookie.match(reg)) {
    return unescape(arr[2]);
  } else {
    return null;
  }
}
